<?php

class Cadouri extends Scaff {

    public function __construct() {
        parent::__construct('cadouri');
        
        if (isset($_GET['add']))
        {
            /*if (!logged_admin()) redirect('login.php');
            if (isset($_POST['submit']))
            {
                $form = new Form($_POST);
                if (
                $form->minlength('nume', 5, 'Nume')
                ) 
                {
                    $post = array();
                    $post['nume'] = mysql_real_escape_string($_POST['nume']);
                    if ($this->add($post)) redirect('cadour.php?all');
                }            
            }
            
            require(view('cadouri_add'));*/
        }
        elseif (isset($_GET['view']))
        {
            if (!logged_admin()) redirect('login.php');
            $object = $this->get('id',$_GET['view']);
            require(view('cadouri_view'));
        }
        elseif (isset($_GET['edit']))
        {
            if (!logged_admin()) redirect('login.php');
            if (isset($_POST['submit']))
            {
                $form = new Form($_POST);
                if (
                $form->minlength('nume', 5, 'Nume')
                ) 
                {
                    $post = array();
                    $post['nume'] = mysql_real_escape_string($_POST['nume']);
                    if ($this->update($post, 'id', $_GET['edit'])) redirect('cadouri.php?all');
                }            
            }
            
            $object = $this->get('id',$_GET['edit']);
            require(view('cadouri_edit'));
        }
        elseif (isset($_GET['del']))
        {
            if (!logged_admin()) redirect('login.php');
            $this->del('id', $_GET['del']);   
        }
        elseif (isset($_GET['all']))
        {
            if (!logged_admin()) redirect('login.php');
            $objects = $this->getAll('id', 'DESC');
            require(view('cadouri_all'));
        }
    }
    
    public function getAll($orderby = '', $direction = 'ASC') {
        return parent::getAll($orderby, $direction);
    }
    
    public function getByPage($limit, $page = 1, $orderby = '', $direction = 'ASC') {
        return parent::getByPage($limit, $page, $orderby, $direction);
    }
    
    public function add($arr) {
        return parent::add($arr);
    }
    
    public function del($field, $value) {
        if (parent::del($field, $value)) redirect('cadouri.php?all');
    }
    
    public function get($field, $value) {
        return parent::get($field, $value);
    }
    
    public function update($arr, $field, $value) {
        return parent::update($arr, $field, $value);
    }
    
}


